<?php

namespace App\Models\Admin\Menus;

class MenusModel extends BaseMenuModel
{
    protected $table = 'menus';

    /**
     * 获取菜单列表
     *
     * @param string $name 路由名称
     * @param array  $ids  主键 ids
     * @return mixed
     */
    public function getMenusList(string $name, array $ids)
    {
        return $this->where('name', $name)
            ->whereIn('id', $ids)
            ->where('deleted', self::DELETED_NO)
            ->get()
            ->toArray();
    }

    /**
     * 获取列表
     *
     * @return mixed
     */
    public function getList(array $fields = self::QUERY_DEFAULT_FIELDS)
    {
        return $this
            ->select($fields)
            ->where('deleted', self::DELETED_NO)
            ->get()
            ->toArray();
    }

    /**
     *根据菜单名称查询菜单
     *
     * @param int    $menuId 菜单id
     * @param string $name   菜单名称
     * @param array  $fields
     * @return array
     */
    public function getMenuByName(int $menuId, string $name, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this->select($fields);
        if ($menuId != 0) {
            $query = $query->where('id', '!=', $menuId);
        }
        $result = $query->where('name', $name)
            ->where('deleted', self::DELETED_NO)
            ->first();

        return $result ? $result->toArray() : [];
    }

    /**
     * 查询子菜单
     *
     * @return array
     */
    public function getItemByParentId(int $parentId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
            ->where('parent_id', $parentId)
            ->where('deleted', self::DELETED_NO)
            ->get()
            ->toArray();
    }

    /**
     * 根据菜单名称查询菜单
     *
     * @param string $name
     * @param array  $fields
     * @return array
     */
    public function getItemByName(string $name, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
            ->where('name', $name)
            ->where('deleted', self::DELETED_NO)
            ->first();
        return $result ? $result->toArray() : [];
    }
}
